<template>
  <div class="login">
    <van-nav-bar>
      <template #left>
        <i class="iconfont f44">&#xe637;</i>
      </template>
    </van-nav-bar>
    <div class="title">你好,请登陆</div>
    <van-form ref="form">
      <van-field
        v-model="form.mobile"
        :rules="rules.mobile"
        name="mobile"
        placeholder="请输入手机号"
      >
        <template #left-icon>
          <i class="iconfont f22">&#xe60b;</i>
        </template>
      </van-field>
      <van-field
        v-model="form.code"
        :rules="rules.code"
        placeholder="请输入验证码"
      >
        <template #left-icon>
          <i class="iconfont f22">&#xe60c;</i>
        </template>
        <template #button>
          <div class="button" @click="getCode" v-if="totalTime === 6">
            获取验证码
          </div>
          <div class="button" v-else @click="getCode">
            还有{{ totalTime }}秒
          </div>
        </template>
      </van-field>
    </van-form>
    <div class="tip">
      登录即同意
      <span>《用户使用协议》</span>
      和<span>《隐私协议》</span>
    </div>
    <van-button type="danger" block round @click="submit">确定</van-button>
  </div>
</template>

<script>
// 布局完成后进行表单验证
// 发送验证码局部验证
// axios准备,导入
// 验证码完善倒计时
// 在响应拦截器对返回的响应做一波失败成功处理
// 成功后保存token

// 导入配置后的axios
// import _fetch from '@/api/request'
// 导入抽取的api
import { auCode, aulogin } from '../../api/login'
// 按需导入
import { setToken } from '@/utils/token'
export default {
  data () {
    return {
      totalTime: 6,
      form: {
        mobile: '',
        code: ''
      },
      rules: {
        // 表单校验 v-model rules
        // validator:(value,rules)=>{return ....},message:'错误信息',trigger:'onChage/onBlur'
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'onChange' },
          {
            validator: value => {
              return /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/.test(
                value
              )
            },
            message: '手机格式不正确',
            trigger: 'onChange'
          }
        ],
        code: [
          { required: true, message: '验证码不能为空', trigger: 'onChange' }
        ]
      }
    }
  },
  methods: {
    // 获取验证码局部校验需要校验的给个name属性  validate(name值)=>{}.then((res)=>{}).catch((err)=>{})
    getCode () {
      // 局部校验
      this.$refs.form
        .validate('mobile')
        .then(() => {
          // 发送请求前表单来个倒计时
          this.totalTime--
          const _time = setInterval(() => {
            this.totalTime--
            if (this.totalTime <= 0) {
              clearInterval(_time)
              this.totalTime = 6
            }
          }, 1000)
          this.$toast.loading({
            message: '获取中...',
            duration: 0
          })
          // this.$toast.success('成功')
          // 下载axios,封装api
          auCode({ mobile: this.form.mobile })
            .then(res => {
              this.$toast.success(res.data.data)
              // 获取到验证码清除倒计时
              clearInterval(_time)
              this.totalTime = 6
            })
            .catch(res => {
              this.$toast.fail(res.data.message)
            })
        })
        .catch(err => {
          this.$toast.fail(err)
        })
    },
    submit () {
      // 全局校验
      this.$refs.form
        .validate()
        .then(() => {
          // this.$toast.success('成功')
          // 下载axios,封装api
          aulogin(this.form)
            .then(res => {
              this.$toast.success(res.data.message)
              // 保存用户信息,设置一个isLogin
              this.$store.commit('setUserInfo', res.data.data.user)
              this.$store.commit('setIsLogin', true)
              // 成功后保存token
              setToken(res.data.data.jwt)
              // 跳转到home页面
              this.$router.push('/home')
            })
            .catch(err => {
              console.log(err)
            })
        })
        .catch(() => {
          this.$toast.fail('验证失败')
        })
    }
  },
  created () {
    console.log(process.env.VUE_APP_URL)
  }
}
</script>
<style lang="less" scoped>
.login {
  padding-left: @p15;
  .f44 {
    font-size: 44px;
  }
  ::v-deep .van-nav-bar__left {
    padding: 0;
  }
  .title {
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Semibold;
    font-weight: 600;
    text-align: left;
    color: #222222;
    line-height: 25px;
    letter-spacing: 0px;
    padding: 50px 0 63px;
  }
  .f22 {
    padding-right: 20px;
  }
  .tip {
    font-size: 12px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #181a39;
    line-height: 16px;
    letter-spacing: 0px;
    padding-top: 27px;
    padding-bottom: 44px;
    span {
      color: #00b8d4;
    }
  }
  .button {
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #00b8d4;
    line-height: 23px;
    letter-spacing: 0px;
  }
}
</style>
